package com.loveazure.util;

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;

public class FileUtil {

	public static Font getFont(String fontName) {
		Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment()
				.getAllFonts();
		for (Font font : fonts) {
			if (font.getFontName().contains(fontName)) {
				return font;
			}
		}
		return null;
	}

	public static String getCharset(File file) {
		String charset = "GBK";
		byte[] first3Bytes = new byte[3];
		BufferedInputStream bis = null;
		try {
			// boolean checked = false;
			bis = new BufferedInputStream(new FileInputStream(file));
			bis.mark(0);
			int read = bis.read(first3Bytes, 0, 3);
			if (read == -1) {
				return charset;
			}
			if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
				charset = "UTF-16LE";
				// checked = true;
			} else if (first3Bytes[0] == (byte) 0xFE
					&& first3Bytes[1] == (byte) 0xFF) {
				charset = "UTF-16BE";
				// checked = true;
			} else if (first3Bytes[0] == (byte) 0xEF
					&& first3Bytes[1] == (byte) 0xBB
					&& first3Bytes[2] == (byte) 0xBF) {
				charset = "UTF-8";
				// checked = true;
			}
			/*******************************************************************
			 * bis.reset(); if (!checked) { int loc = 0; while ((read =
			 * bis.read()) != -1) { loc++; if (read >= 0xF0) { break; } if (0x80
			 * <= read && read <= 0xBF) // 单独出现BF以下的，也算是GBK { break; } if (0xC0
			 * <= read && read <= 0xDF) { read = bis.read(); if (0x80 <= read &&
			 * read <= 0xBF)// 双字节 (0xC0 - 0xDF) { // (0x80 - 0xBF),也可能在GB编码内
			 * continue; } else { break; } } else if (0xE0 <= read && read <=
			 * 0xEF) { // 也有可能出错，但是几率较小 read = bis.read(); if (0x80 <= read &&
			 * read <= 0xBF) { read = bis.read(); if (0x80 <= read && read <=
			 * 0xBF) { charset = "UTF-8"; break; } else { break; } } else {
			 * break; } } } System.out.println(loc + " " +
			 * Integer.toHexString(read)); }
			 ******************************************************************/
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bis != null) {
				try {
					bis.close();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		}
		return charset;
	}

	public static boolean shuffle(String filePath) {
		try {
			File file = new File(filePath);
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			String line;
			ArrayList<String> list = new ArrayList<String>();

			while ((line = br.readLine()) != null) {
				list.add(line);
			}
			fr.close();
			br.close();

			Collections.shuffle(list);
			StringBuffer sb = new StringBuffer();
			for (String s : list) {
				sb.append(s).append("\n");
			}
			FileWriter fw = new FileWriter(file);
			fw.write(sb.toString());
			fw.flush();
			fw.close();

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	public static int countLine(File file) {
		int i = 0;
		try {
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			while (br.ready()) {
				br.readLine();
				i++;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return i;
	}

	public static void copyFile(File sourceFile, File targetFile) {
		try {
			// 新建文件输入流并对它进行缓冲
			FileInputStream input = new FileInputStream(sourceFile);
			BufferedInputStream inBuff = new BufferedInputStream(input);

			// 新建文件输出流并对它进行缓冲
			FileOutputStream output = new FileOutputStream(targetFile);
			BufferedOutputStream outBuff = new BufferedOutputStream(output);

			// 缓冲数组
			byte[] b = new byte[1024 * 5];
			int len;
			while ((len = inBuff.read(b)) != -1) {
				outBuff.write(b, 0, len);
			}
			// 刷新此缓冲的输出流
			outBuff.flush();

			// 关闭流
			inBuff.close();
			outBuff.close();
			output.close();
			input.close();
		} catch (Exception e) {
			// TODO: handle exception
		}

	}
}
